<?php

namespace app\common\library;

// 引入腾讯云OSS SDK相关类
use Qcloud\Cos\Client;
use Qcloud\Cos\Exception\ServiceResponseException;
use Qcloud\Cos\Exception\CosException;

class TencentOss
{
    // 配置信息属性
    protected $secretId;
    protected $secretKey;
    protected $region;
    protected $bucket;

    // 构造函数，用于初始化配置信息
    public function __construct()
    {
        // 从FastAdmin配置文件中获取相关配置（假设配置项名为oss，且在config.php文件中有对应配置）
        $ossConfig = config('oss');
        $this->secretId = $ossConfig['access_key_id'];
        $this->secretKey = $ossConfig['access_key_secret'];
        $this->region = $ossConfig['endpoint'];
        $this->bucket = $ossConfig['bucket'];
    }

    // 初始化OSS客户端
    protected function initClient()
    {
        return new Client([
            'region' => $this->region,
            'schema' => 'https',
            'credentials' => [
                'secretId' => $this->secretId,
                'secretKey' => $this->secretKey
            ]
        ]);
    }

    // 文件上传方法
    public function uploadFile($localFilePath, $objectName)
    {
        $cosClient = $this->initClient();
        try {
            $result = $cosClient->putObject([
                'Bucket' => $this->bucket,
                'Key' => $objectName,
                'Body' => fopen($localFilePath, 'rb')
            ]);
            return $result;
        } catch (ServiceResponseException $e) {
            // 记录日志或者返回错误信息，此处简单返回错误信息示例
            return [
                'success' => false,
                'message' => '文件上传失败，服务端返回错误：'. $e->getMessage()
            ];
        } catch (CosException $e) {
            return [
                'success' => false,
                'message' => '文件上传出现COS异常：'. $e->getMessage()
            ];
        }
    }

    // 文件下载方法
    public function downloadFile($objectName, $localFilePath)
    {
        $cosClient = $this->initClient();
        try {
            $result = $cosClient->getObject([
                'Bucket' => $this->bucket,
                'Key' => $objectName,
                'SaveAs' => $localFilePath
            ]);
            return $result;
        } catch (ServiceResponseException $e) {
            return [
                'success' => false,
                'message' => '文件下载失败，服务端返回错误：'. $e->getMessage()
            ];
        } catch (CosException $e) {
            return [
                'success' => false,
                'message' => '文件下载出现COS异常：'. $e->getMessage()
            ];
        }
    }

    // 文件删除方法
    public function deleteFile($objectName)
    {
        $cosClient = $this->initClient();
        try {
            $result = $cosClient->deleteObject([
                'Bucket' => $this->bucket,
                'Key' => $objectName
            ]);
            return [
                'success' => true,
                'message' => '文件删除成功'
            ];
        } catch (ServiceResponseException $e) {
            return [
                'success' => false,
                'message' => '文件删除失败，服务端返回错误：'. $e->getMessage()
            ];
        } catch (CosException $e) {
            return [
                'success' => false,
                'message' => '文件删除出现COS异常：'. $e->getMessage()
            ];
        }
    }

}